<?php


namespace frontend\controllers;


use yii\helpers\ArrayHelper;
use yii\web\Controller;

class ArrayController extends Controller
{
    public array $value = [
        'user' => [
            'name'   => 'Grey',
            'age'    => 28,
            'weight' => 162,
        ],
    ];

    public function actionIndex()
    {
        
    }
    public function actionKeyExists()
    {
        dump(ArrayHelper::keyExists('user', $this->value));
    }

    public function actionRemove()
    {
        $user = ArrayHelper::remove($this->value, 'user');
        dump($user);
        dump($this->value);
    }

    public function actionSetValue()
    {
        ArrayHelper::setValue($this->value, 'user.name', 'Red');
        dump($this->value);
    }

    public function actionGetValue1()
    {
        $fullName = ArrayHelper::getValue($this->value['user'], function ($user, $defaultValue) {
            return "姓名：{$user['name']}，年龄：{$user['age']}";
        });
        dump($fullName);
    }

    public function actionGetValue()
    {
        $value = ArrayHelper::getValue($this->value, 'user.name', 'red');
        dump($value);
    }
}